// pages/user/register/register.js

var app = getApp();
var i = 60;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        second: i,
        verify: true,
        isset: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('register')
        console.log(getCurrentPages())
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    username:'',
    get_username: function (e) {
          this.username = e.detail.value
    },

    get_verify: function (e) {
        var that = this;
        var username = this.username;
        var info;
        if(username==""){
            info='请输入手机号';
            app.showTopTips(info, that);
            return false;
        }
        app.ajax({
            url: app.host + 'mall/Login/get_verify',
            data: {
                username: username,
            },
            success: function (res) {
                if (res.data.code==0){
                    info = res.data.msg;
                    app.showTopTips(info, that);
                } else {
                    that.setData({
                        isset: false,
                    }); 
                    var timer = setInterval(function () {
                        i--;
                        that.setData({
                            second: i
                        })
                        if (that.data.second == 0) {
                            that.setData({
                                isset: true,
                                verify: false,
                            });
                            i = 60;
                            clearInterval(timer);
                            that.setData({
                                second: i
                            })
                        }
                    }, 1000)
                }
            },
        });
    },

    register: function (e) {
        var username = e.detail.value.username;
        var password = e.detail.value.password;
        var verify = e.detail.value.verify;
        var info;
        var that = this;

        if (!username) {
            info = '未输入手机号';
            app.showTopTips(info, that);
        } else if (!(/^1[3|4|5|7|8][0-9]{9}$/.test(username))) {
            info = '请输入正确的手机号';
            app.showTopTips(info, that);
        } else if (!password) {
            info = '未输入密码';
            app.showTopTips(info, that);
        } else {
            app.ajax({
                url: app.host + 'mall/Login/register',
                data: {
                    username: username,
                    password: password,
                    verify: verify,
                },
                success: function (res) {
                    if (res.data.code == 1) {
                        wx.showModal({
                            content: res.data.msg,
                            showCancel: false,
                            success: function (msg) {
                                if (msg.confirm) {
                                    wx.navigateTo({
                                        url: '/pages/usercenter/shop_info/shop_info',
                                    })
                                }
                            }
                        })
                    } else {
                        info = '注册失败';
                        app.showTopTips(info, that);
                    }
                },
            })
        }
    },
})